package graphics;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import tool.ScreenUtils;

public class SearchInput2 {

	private static final int CHAR_INTERVAL = 2;
	private static final int CHAR_WIDTH = 14;
	private static final char DEFAULT_CHAR = '%';

	private int bgColor = BootColor.BLACK;
	private int brushColor;

	private long RE_PAINT_INTERVAL = 500;
	private int length;
	private int cursorAt = 0;
	private char[] chars = null;
	private Canvas parent = null;
	private boolean cursorVisible = false;
	private int y;
	private Timer repaintTimer;
	private Graphics graphics;

	public SearchInput2(Canvas aParent, int aLength, int aBrushColor) {
		brushColor = aBrushColor;
		length = aLength;
		parent = aParent;
		chars = new char[length];

		for (int i = 0; i < chars.length; i++) {
			chars[i] = DEFAULT_CHAR;
		}

		repaintTimer = new Timer();
		repaintTimer.schedule(new TimerTask() {

			public void run() {
				cursorVisible = !cursorVisible;
				parent.repaint();
			}
		}, RE_PAINT_INTERVAL, RE_PAINT_INTERVAL);
	}

	public void repaint(Graphics g) {
		graphics = g;
		g.setColor(bgColor);
		int xOffset = 3;
		int height = 24;
		int yOffset = g.getFont().getHeight();

		int x = determineX();

		g.drawRoundRect(x, y, CHAR_WIDTH * length + CHAR_INTERVAL * length
				+ xOffset * 2, height + yOffset * 2, 3, 3);

		g.setColor(brushColor);
		int baseX = x + xOffset;

		for (int i = 0; i < length; i++) {
			if (i > 0) {
				if (chars[i] == DEFAULT_CHAR) {
					baseX += CHAR_WIDTH + CHAR_INTERVAL;
				} else {
					baseX += g.getFont().charWidth(
							Character.toUpperCase(chars[i - 1]));
				}
			}

			int baseY = y + yOffset;
			if ((i == cursorAt) && (cursorVisible)) {
				if (chars[i] == DEFAULT_CHAR) {
					g.fillRect(baseX, baseY, CHAR_WIDTH, height);
				} else {
					char upperChar = toUpperchar(chars[i]);
					g.drawChar(upperChar, baseX, baseY + 1, Graphics.LEFT
							| Graphics.TOP);
				}
			} else if (i != cursorAt) {
				if (chars[i] == DEFAULT_CHAR) {
					g.fillRect(baseX, baseY, CHAR_WIDTH, height);
				} else {
					char upperChar = toUpperchar(chars[i]);
					g.drawChar(upperChar, baseX, baseY + 1, Graphics.LEFT
							| Graphics.TOP);
				}
			}
		}
	}

	private char toUpperchar(char c) {
		return String.valueOf(c).toUpperCase().charAt(0);
	}

	private int determineX() {
		int inputWidth = getInputWidth();
		return (ScreenUtils.SCREEN_WIDTH - inputWidth) / 2;
	}

	private int getInputWidth() {
		int result = 0;

		for (int i = 0; i < length; i++) {
			if (chars[i] != DEFAULT_CHAR) {
				result += graphics.getFont().charWidth(chars[i]);
			} else {
				result += CHAR_WIDTH;
			}
		}
		result += CHAR_INTERVAL * (length - 1);
		return result;
	}

	public void charInput(char input) {
		chars[cursorAt] = input;
	}

	public int getCursor() {
		return cursorAt;
	}

	public void setCursorAt(int value) {
		secureSetCursor(value);
	}

	public void shiftCursor(int value) {
		secureSetCursor(cursorAt + value);
	}

	private void secureSetCursor(int value) {
		cursorAt = value;
		if (cursorAt < 0) {
			cursorAt = 0;
		} else if (cursorAt > length - 1) {
			cursorAt = length - 1;
		}
	}

	public void setColors(int aBgColor, int aCursorColor) {
		bgColor = aBgColor;
		brushColor = aCursorColor;
	}

	public void setCharAt(int pos, char value) {
		chars[pos] = value;
	}

	public void setY(int aY) {
		y = aY;
	}
	
	public void freeze() {
		cursorVisible = true;
		if (repaintTimer != null) {
			repaintTimer.cancel();
			repaintTimer = null;
		}
		parent.repaint();
	}
}
